unit PMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, PParentPngForm, GDIPAPI, GDIPOBJ, PBtnPngForm;

type
  TBtnClosePngForm1 = class(TBtnPngForm)
    procedure lblFormClick(Sender: TObject);
  end;

  TMainForm = class(TParentPngForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    imgBackground30: HDC;
    imgBackground31: HDC;
    imgBackground70: HDC;
    imgBackground71: HDC;
  public
    procedure setup;
    function getImgBackground30: HDC;
    function getImgBackground31: HDC;
    function getImgBackground70: HDC;
    function getImgBackground71: HDC;
    function getHDC(dirImage: String): HDC;
    procedure WMNCHitTest (var M: TWMNCHitTest); message wm_NCHitTest;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses PAuthorizationForm, PChildPngForm;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  AuthorizationForm.Close;
end;

procedure TMainForm.setup;
var i: Integer;
begin
  setArrayChilds(nil);

  setParameters('Img/Background_MainForm_3_0.png');

  imgBackground30 := getHDC('Img/Background_MainForm_3_0.png');
  imgBackground31 := getHDC('Img/Background_MainForm_3_1.png');
  imgBackground70 := getHDC('Img/Background_MainForm_7_0.png');
  imgBackground71 := getHDC('Img/Background_MainForm_7_1.png');

  Self.Left := Screen.Width - Self.Width - 10;
  Self.Top := 10;

  SetLengthArrayChilds(2);

  getArrayChilds[0] := TChildPngForm.Create(Self, 0, 'Img/Title.png', 60,10);

  getArrayChilds[1] := TBtnClosePngForm1.Create(Self, 1, 'Img/Close_Normal.png', 'Img/Close_Hover.png', 'Img/Close_Active.png', 240, 13);

  for i := 0 to Length(getArrayChilds) - 1 do
    getArrayChilds[i].Visible := True;

end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  inherited FormCreate(Sender);
  setup;
end;

function TMainForm.getImgBackground30: HDC;
begin
  Result := imgBackground30;
end;

function TMainForm.getImgBackground31: HDC;
begin
  Result := imgBackground31;
end;

function TMainForm.getImgBackground70: HDC;
begin
  Result := imgBackground70;
end;

function TMainForm.getImgBackground71: HDC;
begin
  Result := imgBackground71;
end;

function TMainForm.getHDC(dirImage: String): HDC;
var
  img: TGPBitmap;
  screendc: HDC;
begin
  if SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED) = 0 then
    ShowMessage(SysErrorMessage(GetLastError));

  img := TGPBitmap.Create(dirImage,True);
  with img do begin
    GetHBITMAP(0,hbmp);
    Width := GetWidth;
    Height := GetHeight;
    Free;
  end;
  screendc := GetDC(0);
  backdc := CreateCompatibleDC(screendc);
  SelectObject(backdc, hbmp);

  Result := backdc;
end;

{ TBtnClosePngForm }

procedure TBtnClosePngForm1.lblFormClick(Sender: TObject);
begin
  MainForm.Close;
end;

procedure TMainForm.WMNCHitTest(var M: TWMNCHitTest);
var
  P:TPoint;
begin
  inherited;
  GetCursorPos(p);
  if PtInRect(Rect(Left, Top, Width + Left, Height + Top), P) then
     M.Result := htCaption;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
//  inherited;
  user.showEvent;
end;

end.
